package com.coffeeloversupportlibrary.coffeelover.mercury.main;

import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.ArraySet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;


import com.coffeeloversupportlibrary.R;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author 盛守义
 * @createTime 2021/11/9 16:40
 * @projectName TurningCoffee
 * @className FlowTagActivity.java
 * @description TODO
 */
public class FlowTagActivity extends AppCompatActivity {


    private TagFlowLayout mFlowLayout, wantFlow;
    private EditText et_search;
    private TextView tv_clear;
    private Button btn_search;
    private List<String> strings;
    String history="";
    int a=0;
    //历史记录数据可以保存在本地数据库
    List<String> historylist = new ArrayList<>();
    //获取热门搜索
    List<String> wantlist = new ArrayList<>();
    //布局管理器
    private LayoutInflater mInflater;
    //流式布局的子布局
    private TextView tv;
    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    mFlowLayout.setAdapter(new TagAdapter<String>(strings) {
                        @Override
                        public View getView(FlowLayout parent, int position, String s) {
                            tv = (TextView) mInflater.inflate(R.layout.search_flow_item,
                                    mFlowLayout, false);
                            tv.setVisibility(View.VISIBLE);
                            tv.setText(s);
                            return tv;
                        }
                    });
                    break;

            }
            super.handleMessage(msg);
        }
    };


    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search);
        mInflater = LayoutInflater.from(this);
        mFlowLayout = (TagFlowLayout) findViewById(R.id.history_flow);
        wantFlow = (TagFlowLayout) findViewById(R.id.want_flow);
        et_search = (EditText) findViewById(R.id.edt);
        btn_search = (Button) findViewById(R.id.btn_catesearch);
        tv_clear = (TextView)findViewById(R.id.clear);


        final SharedPreferences preferences = getSharedPreferences("config", 0);
        final SharedPreferences.Editor editor = preferences.edit();

        strings = new ArrayList<>();
        final String string = preferences.getString("string", " ");

        String[] split = string.split("   ");
        if (split.length>0&&!string.equals(" ")){
            for (int i=0;i<split.length;i++){
                strings.add(split[i]);
            }
            handler.sendEmptyMessageDelayed(1, 0);
        }

        //设置搜索框进入活动自动获取焦点
        et_search.setFocusable(true);
        et_search.setFocusableInTouchMode(true);
        et_search.requestFocus();

        wantlist.add("aaaaaa");
        wantlist.add("bbbbbb");
        //设置热门layout
        wantFlow.setAdapter(new TagAdapter<String>(wantlist) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                tv = (TextView) mInflater.inflate(R.layout.search_flow_item,
                        wantFlow, false);
                tv.setVisibility(View.VISIBLE);
                tv.setText(s);
                return tv;
            }
        });

        //清空历史记录
        tv_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                history="";
                historylist.clear();
                editor.clear().commit();
                //清空下面的
                strings.clear();
                handler.sendEmptyMessageDelayed(1, 0);
            }
        });
        //搜索
        btn_search.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onClick(View v) {
                String string = preferences.getString("string", "");

                historylist.clear();

                if (!et_search.getText().toString().trim().equals("")) {
                    String aa = et_search.getText().toString().trim();

                    Set<String> set = new ArraySet<>();
                    set.add(aa);
                    historylist.add(aa);
                    a++;
                    history+= aa+"   ";
                    for (int i=0;i<historylist.size();i++){
                        editor.putString("string",history).commit();
                        strings.add(historylist.get(i));
                    }
                    //通知handler更新UI
                    handler.sendEmptyMessageDelayed(1, 0);
                }else{
                    Toast.makeText(FlowTagActivity.this, "请输入要搜索的内容", Toast.LENGTH_SHORT).show();
                }
            }
        });
        //历史Tag点击事件
        mFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {

                //搜索
                Toast.makeText(FlowTagActivity.this, tv.getText(), Toast.LENGTH_SHORT).show();
                return true;
            }
        });
        //热门Tag点击事件
        wantFlow.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                //搜索
                Toast.makeText(FlowTagActivity.this, tv.getText(), Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }
}

